package com.hxgz.steel.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hxgz.steel.common.ServerResponse;
import com.hxgz.steel.dao.AnnouncementManagerMapper;
import com.hxgz.steel.dao.MessageMapper;
import com.hxgz.steel.dao.UserMsgMapper;
import com.hxgz.steel.domain.AnnouncementManager;
import com.hxgz.steel.domain.Message;
import com.hxgz.steel.domain.Page;
import com.hxgz.steel.domain.UserMsg;
import com.hxgz.steel.service.UserMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("userMessageService")
public class UserMessageServiceImpl implements UserMessageService{

    @Autowired
    private MessageMapper messageMapper;
    @Autowired
    private UserMsgMapper userMsgMapper;
    @Autowired
    private AnnouncementManagerMapper announcementManagerMapper;

    public ServerResponse getMessageList(Page page,Integer status){
        PageHelper.startPage(page.getCurrPageNo(),page.getPageSize());
        List<Message> msgList = messageMapper.getMessageList(status);//0未读消息
        PageInfo pageinfo = new PageInfo(msgList);
        return  ServerResponse.createBySuccess(pageinfo);
    }

    public ServerResponse delMessage(Integer id){
        int count = messageMapper.deleteByPrimaryKey(id);
        if(count >0){
            return  ServerResponse.createBySuccessMessage("删除成功");
        }
        return ServerResponse.createByErrorMessage("删除失败");
    }

    public ServerResponse checkMsg(Long userId,Long id){
        int count = userMsgMapper.selectMsgCount(id);
        if(count>0){
            return ServerResponse.createByErrorMessage("该消息已读过！");
        }
        UserMsg userMsg = new UserMsg();
        userMsg.setUserId(userId);
        userMsg.setMessageId(id);
        userMsg.setStatus(1);
        int i = userMsgMapper.insertSelective(userMsg);
        if (i >0) {
            return  ServerResponse.createBySuccessMessage("保存成功");
        }
        return ServerResponse.createByErrorMessage("保存失败");
    }

    public ServerResponse announcementInfo(){
        List<AnnouncementManager> announcementManager = announcementManagerMapper.getAllList();

        return ServerResponse.createBySuccess(announcementManager);
    }
}
